package com.example.mifi;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class BackgroundView extends View
{
	private Bitmap mMap1, mUndetailedMap1;
	private Bitmap mGlass;
	private Paint mMapPaint;
	private Rect mSrc, mDst;
	
	private int x = 0, y = 0;
	
	public BackgroundView(Context context)
	{
		super(context);
		init(context);
	}
	public BackgroundView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}
	public BackgroundView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context)
	{
		mMap1 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.gray2);
		mGlass = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.glass);
		mGlass = Bitmap.createScaledBitmap(mGlass, mGlass.getWidth()*2, mGlass.getHeight()*2, true);
		
		mUndetailedMap1 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.gray2blur);
		
		mMapPaint = new Paint();
		mMapPaint.setAntiAlias(true);
		mMapPaint.setFilterBitmap(true);
		mMapPaint.setDither(true);
		mMapPaint.setTextSize(25);
		mMapPaint.setColor(Color.RED);
		
		mSrc = new Rect();
		mDst = new Rect();
	} 
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		x++;
		y = 0;
		if (x > mMap1.getWidth())
			x = -canvas.getWidth();
		mSrc.set(x, y, x + canvas.getWidth(), y + canvas.getHeight());
		
		mDst.set(0, 0, canvas.getWidth(), canvas.getHeight());
		canvas.drawBitmap(mUndetailedMap1, mSrc, mDst, mMapPaint);
		
		//x=20/216, y=20/148, 180/216, 90/148 
		int glx = 100, gly = 100;
		mSrc.set(x + (int)(glx+mGlass.getWidth()*20./216), 
				y + (int)(gly+mGlass.getHeight()*20./148), 
				x + (int)(glx+mGlass.getWidth()*20./216+mGlass.getWidth()*180./216), 
				y + (int)(gly+mGlass.getHeight()*20./148+mGlass.getHeight()*90./148));
		mDst.set((int)(glx+mGlass.getWidth()*20./216), 
				(int)(gly+mGlass.getHeight()*20./148), 
				(int)(glx+mGlass.getWidth()*20./216+mGlass.getWidth()*180./216), 
				(int)(gly+mGlass.getHeight()*20./148+mGlass.getHeight()*90./148));
		canvas.drawBitmap(mMap1, mSrc, mDst, mMapPaint);
		
		//mDst.set(glx, gly, glx+mGlass.getWidth(), gly+mGlass.getHeight());
		//canvas.drawBitmap(mGlass, null, mDst, mMapPaint);
		invalidate();
	}
}
